package com.lier.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lier.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.lier.gulimall.product.dao.AttrDao;
import com.lier.gulimall.product.dao.CategoryDao;
import com.lier.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.lier.gulimall.product.entity.AttrEntity;
import com.lier.gulimall.product.vo.*;
import com.sun.org.apache.bcel.internal.generic.LMUL;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lier.common.utils.PageUtils;
import com.lier.common.utils.Query;

import com.lier.gulimall.product.dao.AttrGroupDao;
import com.lier.gulimall.product.entity.AttrGroupEntity;
import com.lier.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Resource
    private AttrDao attrDao;
    @Resource
    private AttrGroupDao attrGroupDao;
    @Resource
    private AttrGroupService attrGroupService;
    @Resource
    private AttrAttrgroupRelationServiceImpl attrAttrgroupRelationService;
    @Resource
    private AttrServiceImpl attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        if(catelogId == 0){
            LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<>();
            if(!StringUtils.isEmpty(params.get("key"))){
                wrapper.eq(AttrGroupEntity::getAttrGroupId,params.get("key"))
                        .or().like(AttrGroupEntity::getAttrGroupName,params.get("key"));
            }
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }else {
            // 获取检索条件
            String key = (String)params.get("key");
            LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<>();
            if(key != null && !key.isEmpty()){
                wrapper.eq(AttrGroupEntity::getCatelogId,catelogId);
                wrapper.and(conditional -> {
                    conditional.eq(AttrGroupEntity::getAttrGroupId,key).or()
                            .like(AttrGroupEntity::getAttrGroupName,key);
                });
            }else {
                wrapper.eq(AttrGroupEntity::getCatelogId,catelogId);
            }
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrEntity> getRelationAttr(Long attrgroupId) {
        LambdaQueryWrapper<AttrAttrgroupRelationEntity> attrAttrgroupRelationWrapper = new LambdaQueryWrapper<>();
        attrAttrgroupRelationWrapper.eq(AttrAttrgroupRelationEntity::getAttrGroupId,attrgroupId);
        List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationDao.selectList(attrAttrgroupRelationWrapper);

        List<Long> allAttrId = relationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());

        LambdaUpdateWrapper<AttrEntity> attrEntityLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        attrEntityLambdaUpdateWrapper.in(AttrEntity::getAttrId,allAttrId);
        return attrDao.selectList(attrEntityLambdaUpdateWrapper);
    }

    @Override
    public void removeAttrRelation(List<AttrRelationVo> attrRelationVoList) {

        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrRelationVoList.stream().map(item -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrId(item.getAttrId());
            attrAttrgroupRelationEntity.setAttrGroupId(item.getAttrGroupId());
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        attrAttrgroupRelationDao.deleteBatchRelation(attrAttrgroupRelationEntities);
    }

    @Override
    public void saveRelation(List<AttrRelationVo> attrRelationVoList) {
        attrRelationVoList.forEach(item -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, attrAttrgroupRelationEntity);
            attrAttrgroupRelationDao.insert(attrAttrgroupRelationEntity);
        });

    }

    @Override
    public List<AttrGroupWithAttrsVo> getGroupWithAttrs(Long catelogId) {

        // 1: 获取当前分类中的所有分组
        List<AttrGroupEntity> attrGroupEntities = baseMapper.selectList(new LambdaQueryWrapper<AttrGroupEntity>()
                .eq(AttrGroupEntity::getCatelogId, catelogId));

        if(attrGroupEntities == null || attrGroupEntities.isEmpty()){
            return null;
        }


        // 2: 构建返回的数据
        return attrGroupEntities.stream().map(item -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item, attrsVo);

            // 获取当前分组的属性集合
            List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationDao
                    .selectList(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                            .eq(AttrAttrgroupRelationEntity::getAttrGroupId, item.getAttrGroupId()));

            if(attrAttrgroupRelationEntities == null || attrAttrgroupRelationEntities.isEmpty()){
                attrsVo.setAttrs(null);
                return attrsVo;
            }

            List<Long> attrIdList = attrAttrgroupRelationEntities.stream()
                    .map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());

            // 查询当前属性的集合
            List<AttrEntity> entities = attrDao.selectList(new LambdaQueryWrapper<AttrEntity>().in(AttrEntity::getAttrId, attrIdList));
            attrsVo.setAttrs(entities);
            return attrsVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<SpuItemBaseAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        // 查询当前spu对应的所有属性的分组信息已经当前分组下所有属性对应的值
        return baseMapper.getAttrGroupWithAttrsBySpuIdAndCatalogId(spuId, catalogId);
    }

}
